package com.zhou.bjgl.bjgl.resp.common;


import com.zhou.bjgl.bjgl.exception.BwExceptionEnum;

import java.io.Serializable;

/*
* 通用返回对象
* */
public class ApiRestResponse<T> implements Serializable {
    Integer code;
    String msg;
    T data;

    private static final int OK_CODE=20000;
    private static final String OK_MSG="SUCCESS";

    public ApiRestResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ApiRestResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    //默认没有信息传入
    public ApiRestResponse() {
        this(OK_CODE,OK_MSG);
    }

    public static <T> ApiRestResponse<T> success(){
        return new ApiRestResponse<>();
    }

    public static <T> ApiRestResponse<T> success(T result){
        ApiRestResponse<T> response = new ApiRestResponse<>();
        response.setData(result);
        return response;
    }

    public static <T> ApiRestResponse<T> error(Integer code, String msg){

        return new ApiRestResponse<>(code,msg);

    }

    public static <T> ApiRestResponse<T> error(BwExceptionEnum ex){

        return new ApiRestResponse<>(ex.getCode(),ex.getMsg());

    }


    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String toString() {
        return "ApiRestResponse{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
